package com.gzyj.flink.dto.alarm;

import com.gzyj.flink.dto.utils.DateUtils;
import com.gzyj.flink.dto.utils.ToolUtils;
import com.gzyj.flink.gps.GPSPosition;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

@Data
public class TotalFatigueDrivingData implements Serializable {


    /// <summary>
    /// 不受Reset控制
    /// </summary>
    public Date LocalTime = DateUtils.MIN_VALUE();
    public Date GpsTime = DateUtils.MIN_VALUE();

    /// <summary>
    /// 判断车辆数据是否开始计算
    /// </summary>
    public boolean IsCalc = false;
    /// <summary>
    /// 满足条件开始时间
    /// </summary>
    public Date ConditionBeginTime = DateUtils.MIN_VALUE();
    /// <summary>
    /// 满足条件开始时经度
    /// </summary>
    public double ConditionBeginLongitude = 0;
    /// <summary>
    /// 满足条件开始时纬度
    /// </summary>
    public double ConditionBeginLatitude = 0;
    /// <summary>
    /// 满足条件开始时速度
    /// </summary>
    public int ConditionBeginSpeed = 0;
    /// <summary>
    /// 满足条件结束时间
    /// </summary>
    public Date ConditionEndTime = DateUtils.MIN_VALUE();
    /// <summary>
    /// 满足条件结束时经度
    /// </summary>
    public double ConditionEndLongitude = 0;
    /// <summary>
    /// 满足条件结束时纬度
    /// </summary>
    public double ConditionEndLatitude = 0;
    /// <summary>
    /// 满足条件结束时速度
    /// </summary>
    public int ConditionEndSpeed = 0;

    /// <summary>
    /// 是否正在报警
    /// </summary>
    public boolean IsAlarm = false;
    /// <summary>
    /// 报警开始时间
    /// </summary>
    public Date AlarmBeginTime = DateUtils.MIN_VALUE();
    /// <summary>
    /// 报警开始时经度
    /// </summary>
    public double AlarmBeginLongitude = 0;
    /// <summary>
    /// 报警开始时纬度
    /// </summary>
    public double AlarmBeginLatitude = 0;
    /// <summary>
    /// 报警结束时间
    /// </summary>
    public Date AlarmEndTime = DateUtils.MIN_VALUE();
    /// <summary>
    /// 报警结束时经度
    /// </summary>
    public double AlarmEndLongitude = 0;
    /// <summary>
    /// 报警结束时纬度
    /// </summary>
    public double AlarmEndLatitude = 0;
    /// <summary>
    /// 报警持续过程中的最大速度
    /// </summary>
    public int AlarmMaxSpeed = 0;
    /// <summary>
    /// 报警持续里程
    /// </summary>
    public double AlarmDistance = 0;

    /// <summary>
    /// 最后发送预警短信得时间
    /// </summary>
    public Date WarningContentSendTime = DateUtils.MIN_VALUE();
    /// <summary>
    /// 发送预警短信得次数
    /// </summary>
    public int WarningContentCount = 0;
    /// <summary>
    /// 最后发送报警短信得时间
    /// </summary>
    public Date AlarmContentSendTime = DateUtils.MIN_VALUE();
    /// <summary>
    /// 发送报警短信得次数
    /// </summary>
    public int AlarmContentCount = 0;
    /// <summary>
    /// GUID
    /// 单独生成 不受Reset控制
    /// </summary>
    public String SingleGuid = UUID.randomUUID().toString();

    public ConcurrentHashMap<String, Double> OnceDayTotalRunTime = new ConcurrentHashMap<>();


    /**
     * 开始计算
     *
     * @param gps
     */
    public void StartCalc(GPSPosition gps) {
        this.IsCalc = true;
        this.ConditionBeginTime = gps.getGPSTimeExt();
        this.ConditionBeginLongitude = gps.getLngExt();
        this.ConditionBeginLatitude = gps.getLatExt();
        this.ConditionBeginSpeed = gps.getSpeedExt();
        this.ConditionEndTime = gps.getGPSTimeExt();
        this.ConditionEndLongitude = gps.getLngExt();
        this.ConditionEndLatitude = gps.getLatExt();
        this.ConditionEndSpeed = gps.getSpeedExt();
        //this.AlarmGuid = Guid.NewGuid().ToString();
        if (OnceDayTotalRunTime.containsKey(DateUtils.getTotay())) {
            OnceDayTotalRunTime.put(DateUtils.getTotay(), 0d);
        } else {
            OnceDayTotalRunTime.clear();
            OnceDayTotalRunTime.put(DateUtils.getTotay(), 0d);
        }
    }

    /// <summary>
    /// 记录计算过程中的数据
    /// </summary>
    public void WriteCalc(GPSPosition gps) {
        //double sumTime = 0;
        //if (jT808_0x0200.SpeedExt() > 15)
        //{
        //    TimeSpan ts = jT808_0x0200.GPSTime - this.ConditionEndTime;
        //    sumTime = ts.TotalSeconds;
        //}
        this.ConditionEndTime = gps.getGPSTimeExt();
        this.ConditionEndLongitude = gps.getLngExt();
        this.ConditionEndLatitude = gps.getLatExt();
        this.ConditionEndSpeed = gps.getSpeedExt();
        //
        //OnceDayTotalRunTime[DateTime.Now.Date] = OnceDayTotalRunTime[DateTime.Now.Date] + sumTime;
    }

    public void StartAlarm(GPSPosition gps) {
        this.IsAlarm = true;
        this.AlarmBeginTime = gps.getGPSTimeExt();
        this.AlarmBeginLongitude = gps.getLngExt();
        this.AlarmBeginLatitude = gps.getLatExt();
        this.AlarmEndTime = gps.getGPSTimeExt();
        this.AlarmEndLongitude = gps.getLngExt();
        this.AlarmEndLatitude = gps.getLatExt();
        this.AlarmMaxSpeed = gps.getSpeedExt();
        this.AlarmDistance = 0;
    }

    /// <summary>
    /// 重置计算数据
    /// </summary>
    public void ResetData() {
        this.IsCalc = false;
        this.ConditionBeginTime = DateUtils.MIN_VALUE();
        this.ConditionBeginLongitude = 0;
        this.ConditionBeginLatitude = 0;
        this.ConditionBeginSpeed = 0;
        this.ConditionEndTime = DateUtils.MIN_VALUE();
        this.ConditionEndLongitude = 0;
        this.ConditionEndLatitude = 0;
        this.ConditionEndSpeed = 0;

        this.IsAlarm = false;
        this.AlarmBeginTime = DateUtils.MIN_VALUE();
        this.AlarmBeginLongitude = 0;
        this.AlarmBeginLatitude = 0;
        this.AlarmEndTime = DateUtils.MIN_VALUE();
        this.AlarmEndLongitude = 0;
        this.AlarmEndLatitude = 0;
        this.AlarmMaxSpeed = 0;
        this.AlarmDistance = 0;
        this.WarningContentSendTime = DateUtils.MIN_VALUE();
        this.WarningContentCount = 0;
        this.AlarmContentSendTime = DateUtils.MIN_VALUE();
        this.AlarmContentCount = 0;
        this.OnceDayTotalRunTime = new ConcurrentHashMap<>();
    }


    /// <summary>
    /// 生成GUID
    /// </summary>
    public void GenerateSingleGuid() {
        this.SingleGuid = UUID.randomUUID().toString();
    }

    /// <summary>
    /// 结束报警
    /// </summary>
    public void StopAlarm(GPSPosition gps) {
        this.IsAlarm = false;
        double lon = this.AlarmEndLongitude;
        double lat = this.AlarmEndLatitude;
        this.AlarmEndTime = gps.getGPSTimeExt();
        this.AlarmEndLongitude = gps.getLngExt();
        this.AlarmEndLatitude = gps.getLatExt();
        this.AlarmMaxSpeed = Math.max(this.AlarmMaxSpeed, gps.getSpeedExt());
        this.AlarmDistance = this.AlarmDistance + ToolUtils.GetDistance(lon, lat, gps.getLngExt(), gps.getLatExt());
    }

}
